from django.db import models
from django.contrib.auth.models import User
from book.models import Book
from shortuuidfield import ShortUUIDField

# Create your models here.
class Profile(models.Model):

    uid = ShortUUIDField(max_length=32, primary_key=True, unique=True, verbose_name='用户唯一标识')
    phone = models.CharField(max_length=11, blank=True, null=True, verbose_name='用户电话', db_index=True)
    avatar = models.CharField(max_length=60, blank=True, null=True, verbose_name='用户头像')

    # 会员相关
    is_upgrade = models.IntegerField(default=0, verbose_name='是否升级会员')
    upgrade_time = models.DateTimeField(blank=True, null=True, verbose_name='升级日期')
    expire_time = models.DateTimeField(blank=True, null=True, verbose_name='过期时间')
    upgrade_count = models.IntegerField(default=0, verbose_name='升级次数')
    # 关联用户表
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    # 关联书籍表
    collect_books = models.ManyToManyField(Book, related_name='collecting_profiles', db_table='profile_collect_book', blank=True)  # 收藏表
    owned_books = models.ManyToManyField(Book, related_name='owning_profiles', db_table='profile_owned_book', blank=True)          # 所属表

    class Meta:
        db_table = 'profile'
        verbose_name = '用户信息'
        verbose_name_plural = '用户信息'